package com.corner.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.corner.R;
import com.corner.model.db.User;
import com.corner.ui.activity.SelectContactActivity;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2015/7/22，00.49.
 * 描述：
 */
public class SelectContactAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<User> users;

    public SelectContactAdapter(Context context, List<User> users) {
        this.context = context;
        this.users = users;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lv_select_contact_item, null);
            Holder holder = new Holder();
            InjectViewUtils.injectView(holder, convertView, context);
            convertView.setTag(holder);
        }
        Holder holder = (Holder) convertView.getTag();
        User user = users.get(position);
        if (user.getAvatar() != null)
            holder.ivAvater.setImageURI(Uri.parse(user.getAvatar().getFileUrl()));
        holder.tvName.setText(user.getUsername());
        convertView.setOnClickListener(this);
        convertView.setTag(R.integer.select_contact_view_user, user);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        User user = (User) v.getTag(R.integer.select_contact_view_user);
        ((SelectContactActivity) context).selectUser(user);
    }

    class Holder {
        @FindView(R.id.iv_select_contact_avater)
        SimpleDraweeView ivAvater;
        @FindView(R.id.tv_select_contact_nickname)
        TextView tvName;
    }
}
